#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    int findTheDistanceValue(vector<int> &arr1, vector<int> &arr2, int d)
    {
        sort(arr2.begin(), arr2.end());
        int ret = 0;
        for (auto e : arr1)
        {
            int l = 0, r = arr2.size() - 1;
            while (l < r)
            {
                int mid = (l + r + 1) / 2;
                if (arr2[mid] <= e)
                {
                    l = mid;
                }
                else
                {
                    r = mid - 1;
                }
            }
            if (abs(e - arr2[l]) <= d)
            {
                continue;
            }
            l = 0, r = arr2.size() - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (arr2[mid] >= e)
                {
                    r = mid;
                }
                else
                {
                    l = mid + 1;
                }
            }
            if (abs(e - arr2[l]) <= d)
            {
                continue;
            }
            ++ret;
        }
        return ret;
    }
};